package com.henghe.data_process.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.henghe.data_process.entity.TBdMaterialpurinfo;
import com.henghe.data_process.entity.TPmPplentry;
import com.henghe.data_process.entity.TPmPurpricelist;
import com.henghe.data_process.mapper.TBdMaterialMapper;
import com.henghe.data_process.mapper.TBdMaterialpurinfoMapper;
import com.henghe.data_process.mapper.TPmPplentryMapper;
import com.henghe.data_process.mapper.TPmPurpricelistMapper;
import com.henghe.frame.domain.base.BaseClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2025/6/3 15:34
 */
@Service
@Slf4j
public class MaterialService extends BaseClass {


    @Autowired
    private TBdMaterialMapper tBdMaterialMapper;

    @Autowired
    private TBdMaterialpurinfoMapper tBdMaterialpurinfoMapper;

    @Autowired
    private TPmPplentryMapper tPmPplentryMapper;

    @Autowired
    private TPmPurpricelistMapper tPmPurpricelistMapper;


    public List getList() {
        tBdMaterialMapper.selectList(null);
        tBdMaterialpurinfoMapper.selectList(null);
        tPmPplentryMapper.selectList(null);
        tPmPurpricelistMapper.selectList(null);
        return null;
    }

    public void handleMaterialData() {
        List<TPmPurpricelist> tPmPurpricelists = tPmPurpricelistMapper.selectList(new LambdaQueryWrapper<TPmPurpricelist>().eq(TPmPurpricelist::getFnumber,"CGJMB-231228-0012"));
//        List<TPmPurpricelist> tPmPurpricelists = tPmPurpricelistMapper.selectList(null);
        for (TPmPurpricelist t : tPmPurpricelists) {
            List<TPmPplentry> entries = tPmPplentryMapper.selectList(new LambdaQueryWrapper<TPmPplentry>().eq(TPmPplentry::getFid, t.getFid()));
            if (entries.isEmpty()) {
                System.out.println(t.getFid() + "  分录为空");
                continue;
            }
            for (TPmPplentry entry : entries) {
                TBdMaterialpurinfo tt = tBdMaterialpurinfoMapper.selectOne(new LambdaQueryWrapper<TBdMaterialpurinfo>()
                        .eq(TBdMaterialpurinfo::getFid, entry.getFmaterialid()));
                if (tt == null) {
                    System.out.println(entry.getFmaterialid() + "  未找到对应的物料");
                    continue;
                }
                if (tt.getFcreateorgid() == null) {
                    System.out.println(entry.getFmaterialid() + "  未找到对应的物料2");
                    continue;
                }
                if (tt.getFcreateorgid().equals(t.getForgid())) {
                    continue;
                }
                List<TBdMaterialpurinfo> tBdMaterialpurinfos = tBdMaterialpurinfoMapper.selectList(new LambdaQueryWrapper<TBdMaterialpurinfo>()
                        .eq(TBdMaterialpurinfo::getFcreateorgid, t.getForgid())
                        .eq(TBdMaterialpurinfo::getFmasterid, tt.getFmasterid()));
                if (tBdMaterialpurinfos.isEmpty()) {
                    System.out.println(entry.getFmaterialid() + "  未找到对应的物料3");
                    continue;
                }
                entry.setFmaterialid(tBdMaterialpurinfos.get(0).getFid());
                tPmPplentryMapper.updateById(entry);
            }
        }
    }
}
